﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using DevExpress.XtraEditors;
using MM.Common.Base;
using DevExpress.XtraEditors.Controls;
using DevExpress.Utils;
using System.Windows.Forms;
using System.Data;
using DevExpress.XtraEditors.Drawing;

namespace MM.Common.UI.Controls
{
    [Obsolete("请使用UCComboData(绑定数据下拉), UCComboFromDict(绑定数据字典下拉) 或 UCComboFromTable(绑定某表下拉)")]
    public class UCComboDictBox : LookUpEdit, IControlProperty, IControlField
    {
        // Fields
        private bool _AllowEmpty = true;
        private string _ControlCode;
        private string _ControlName;
        private EControlPropertyStatus _ControlProperty;
        private string _DefaultData = "";
        private bool _ExistListData;
        private string _FieldDBName = "";
        private EControlDBType _fieldType;
        private bool _isFake;
        private bool _isIndex;
        private string _ShowTab = "";
        private string _ShowTabCode = "";
        private string _ShowTabID = "";
        private string _ShowTabOrder = "";
        private string _ShowTabText = "";
        private string _ShowTabValue = "";
        private string _ShowTabWhere = "";
        private IContainer components;
        private bool isedited;
        private string showcode = "";

        // Methods
        public UCComboDictBox()
        {
            this.InitializeComponent();
        }

        public void clearMe()
        {
            this.ItemIndex = -1;
            this.EditValue = null;
            this.isEdited = false;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public void init()
        {
            base.Properties.NullText = "";
            this._ControlProperty = EControlPropertyStatus.Read;
            base.ErrorIconAlignment = ErrorIconAlignment.TopRight;
            base.Properties.ValueMember = "VALUE";
            base.Properties.DisplayMember = "TEXT";
            LookUpColumnInfo column = new LookUpColumnInfo
            {
                FieldName = "TEXT",
                Caption = "内容"
            };
            base.Properties.Columns.Add(column);
            LookUpColumnInfo info2 = new LookUpColumnInfo
            {
                FieldName = "CODE",
                Caption = "输入码",
                Width = 6
            };
            base.Properties.Columns.Add(info2);
            LookUpColumnInfo info3 = new LookUpColumnInfo
            {
                FieldName = "VALUE",
                Visible = false
            };
            base.Properties.Columns.Add(info3);
            LookUpColumnInfo info4 = new LookUpColumnInfo
            {
                FieldName = "ID",
                Visible = false
            };
            base.Properties.Columns.Add(info4);
            base.Properties.ShowHeader = false;
            base.Properties.TextEditStyle = TextEditStyles.Standard;
            base.Properties.AutoSearchColumnIndex = 1;
            base.Properties.SearchMode = SearchMode.AutoComplete;
            base.Properties.ImmediatePopup = true;
            base.Properties.CloseUpKey = new KeyShortcut(Keys.Shift | Keys.F4);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected override void OnClickButton(EditorButtonObjectInfoArgs buttonInfo)
        {
            base.OnClickButton(buttonInfo);
            if (!this.IsPopupOpen)
            {
                if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                {
                    SendKeys.Send("{F4}");
                }
                else
                {
                    SendKeys.Send("+{F4}");
                }
            }
        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            if ((!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty != EControlPropertyStatus.Write)) && base.Enabled)
            {
                base.Enabled = false;
            }
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
            {
                SendKeys.Send("{F4}");
            }
            else
            {
                SendKeys.Send("+{F4}");
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if ((!this.IsPopupOpen && !e.Shift) && (this._ExistListData && !base.Properties.ReadOnly))
            {
                string str = char.ConvertFromUtf32(e.KeyValue);
                if (char.IsLetterOrDigit(str[0]) && (str.Length == 1))
                {
                    SendKeys.Send("{F4}");
                    SendKeys.SendWait("{" + str[0] + "}");
                }
            }
            base.OnKeyDown(e);
        }

        protected override void OnLeave(EventArgs e)
        {
        }

        protected override void OnLoaded()
        {
            base.OnLoaded();
            if (!base.DesignMode)
            {
                this.init();
                this.setListValue();
            }
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);
            this.isEdited = this.IsModified;
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if ((!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty == EControlPropertyStatus.Hide)) && base.Visible)
            {
                base.Visible = false;
            }
        }

        public void setListValue()
        {
            string commandtext = "";
            base.Properties.DataSource = null;
            if ((!string.IsNullOrEmpty(this.ShowTab) && !string.IsNullOrEmpty(this.ShowTabValue)) && (SystemInfo.DbData != null))
            {
                if (string.IsNullOrEmpty(this.ShowTabText))
                {
                    this.ShowTabText = this.ShowTabValue;
                }
                if (SystemInfo.DbData.IsOpen)
                {
                    DataTable dt = new DataTable();
                    if (this.ShowTab.Trim().ToUpper() == "MXHLY_SYS_DICTIONARY")
                    {
                        if (SystemInfo.OperInputCode == "1")
                        {
                            this.showcode = "INPUTCODE1";
                        }
                        else
                        {
                            this.showcode = "INPUTCODE2";
                        }
                        commandtext = "SELECT DT_CODE as VALUE,DT_NAME as TEXT," + this.showcode + " as CODE,DT_ID as ID, DT_DEFAULT_IS FROM MXHLY_SYS_DICTIONARY where DT_USE='Y' AND DT_TYPE='" + this.ShowTabValue.Trim() + "' " + this.Where + " order by DT_SORT ";
                        SystemInfo.DbData.Retrieve(commandtext, ref dt);
                        if (this._AllowEmpty)
                        {
                            DataRow row = dt.NewRow();
                            row["CODE"] = " ";
                            row["VALUE"] = "";
                            dt.Rows.Add(row);
                        }
                    }
                    else
                    {
                        string str2 = "";
                        string str3 = "";
                        string str4 = "";
                        if (!string.IsNullOrEmpty(this.ShowTabCode))
                        {
                            if (SystemInfo.OperInputCode == "1")
                            {
                                this.showcode = "," + this.ShowTabCode + "1 as CODE";
                            }
                            else
                            {
                                this.showcode = "," + this.ShowTabCode + "2 as CODE";
                            }
                        }
                        if (!string.IsNullOrEmpty(this.Order))
                        {
                            str2 = " order by " + this.Order + " ";
                        }
                        if (!string.IsNullOrEmpty(this.Where))
                        {
                            if (this.Where.ToUpper().IndexOf("WHERE") != -1)
                            {
                                str4 = this.Where ?? "";
                            }
                            else
                            {
                                str4 = " WHERE " + this.Where + " ";
                            }
                        }
                        if (!string.IsNullOrEmpty(this.ShowTabID))
                        {
                            str3 = "," + this.ShowTabID + " as ID";
                        }
                        commandtext = "SELECT " + this.ShowTabValue + " as VALUE," + this.ShowTabText + " as TEXT " + str3 + " " + this.showcode + " FROM " + this.ShowTab + " " + str4 + str2;
                        SystemInfo.DbData.Retrieve(commandtext, ref dt);
                        if (this._AllowEmpty && (this.showcode != string.Empty))
                        {
                            DataRow row2 = dt.NewRow();
                            row2[3] = " ";
                            //row2["Code"] = " "; 
                            row2["VALUE"] = DBNull.Value;
                            dt.Rows.Add(row2);
                        }
                    }
                    base.Properties.DataSource = dt;
                    this._ExistListData = dt.Rows.Count > 0;
                }
            }
            if (!string.IsNullOrEmpty(this.DefaultData))
            {
                this.EditValue = this.DefaultData.Trim();
            }
        }

        // Properties
        [DefaultValue(true), Browsable(true), Category("MM附加"), Description("本控件是否允许有空值")]
        public bool AllowEmpty
        {
            get
            {
                return this._AllowEmpty;
            }
            set
            {
                this._AllowEmpty = value;
            }
        }

        [Category("MM附加"), DefaultValue((string)null), Browsable(true), Description("控制本控件对应的系统代码")]
        public string ControlCode
        {
            get
            {
                return this._ControlCode;
            }
            set
            {
                this._ControlCode = value;
            }
        }

        [Description("控制本控件对应的系统名称"), Browsable(true), Category("MM附加"), DefaultValue((string)null)]
        public string ControlName
        {
            get
            {
                return this._ControlName;
            }
            set
            {
                this._ControlName = value;
                if (!base.DesignMode && !string.IsNullOrEmpty(this.ControlCode))
                {
                    if (this._ControlProperty == EControlPropertyStatus.None)
                    {
                        this._ControlProperty = BaseDataHelper.GetFunctionPointProperty(this._ControlCode, this._ControlName);
                    }
                    switch (this._ControlProperty)
                    {
                        case EControlPropertyStatus.Read:
                            base.Properties.ReadOnly = true;
                            base.Visible = true;
                            return;

                        case EControlPropertyStatus.Write:
                            base.Properties.ReadOnly = false;
                            base.Visible = true;
                            return;
                    }
                    base.Visible = false;
                }
            }
        }

        [Browsable(false), DefaultValue(0)]
        public EControlPropertyStatus ControlProperty
        {
            get
            {
                return this._ControlProperty;
            }
            set
            {
            }
        }

        [Browsable(true), DefaultValue(""), Category("字典表"), Description("本控件在新增时的缺省值")]
        public string DefaultData
        {
            get
            {
                return this._DefaultData;
            }
            set
            {
                this._DefaultData = value;
            }
        }

        [Browsable(true), Category("MM数据库"), Description("本控件在数据库中对应的字段名"), DefaultValue("")]
        public string FieldDBName
        {
            get
            {
                return this._FieldDBName;
            }
            set
            {
                this._FieldDBName = value;
            }
        }

        [Browsable(true), Category("MM数据库"), Description("字段在数据库中的类型"), DefaultValue(0)]
        public EControlDBType FieldType
        {
            get
            {
                return this._fieldType;
            }
            set
            {
                this._fieldType = value;
            }
        }

        [DefaultValue((string)null), Category("MM数据库"), Description("字段的值"), Browsable(true)]
        public object FieldValue
        {
            get
            {
                return this.EditValue;
            }
            set
            {
                if (value != null)
                {
                    this.EditValue = value;
                }
            }
        }

        [Description("将编辑控件进行分组管理"), Category("MM数据绑定"), Browsable(true), DefaultValue((string)null)]
        public string GroupName { get; set; }

        [Browsable(false), DefaultValue(false)]
        public bool isEdited
        {
            get
            {
                return this.isedited;
            }
            set
            {
                this.isedited = value;
            }
        }

        [Browsable(true), Category("MM数据库"), Description("在数据库中并不存在，但在界面中存在的显示控件"), DefaultValue(false)]
        public bool IsFake
        {
            get
            {
                return this._isFake;
            }
            set
            {
                this._isFake = value;
            }
        }

        [Browsable(true), Category("MM数据库"), Description("该字段是否主键"), DefaultValue(false)]
        public bool IsIndex
        {
            get
            {
                return this._isIndex;
            }
            set
            {
                this._isIndex = value;
            }
        }

        [Description("字典表排序字段名称"), DefaultValue(""), Category("字典表"), Browsable(true)]
        public string Order
        {
            get
            {
                return this._ShowTabOrder;
            }
            set
            {
                this._ShowTabOrder = value;
            }
        }

        [Category("字典表"), Description("字典表表名"), DefaultValue(""), Browsable(true)]
        public string ShowTab
        {
            get
            {
                return this._ShowTab;
            }
            set
            {
                this._ShowTab = value;
            }
        }

        [Category("字典表"), Description("字典表Code字段名称"), DefaultValue(""), Browsable(true)]
        public string ShowTabCode
        {
            get
            {
                return this._ShowTabCode;
            }
            set
            {
                this._ShowTabCode = value;
            }
        }

        [Description("字典表ID字段名称"), Category("字典表"), Browsable(true), DefaultValue("")]
        public string ShowTabID
        {
            get
            {
                return this._ShowTabID;
            }
            set
            {
                this._ShowTabID = value;
            }
        }

        [Category("字典表"), Browsable(true), DefaultValue(""), Description("字典表Text字段名称")]
        public string ShowTabText
        {
            get
            {
                return this._ShowTabText;
            }
            set
            {
                this._ShowTabText = value;
            }
        }

        [Category("字典表"), Description("字典表Value字段名称"), DefaultValue(""), Browsable(true)]
        public string ShowTabValue
        {
            get
            {
                return this._ShowTabValue;
            }
            set
            {
                this._ShowTabValue = value;
            }
        }

        [Category("字典表"), Browsable(true), DefaultValue(""), Description("字典表Where条件")]
        public string Where
        {
            get
            {
                return this._ShowTabWhere;
            }
            set
            {
                this._ShowTabWhere = value;
            }
        }
    }
}
